package parsing;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ParsingCitiesToTextB {

	public static int numOfOutputsFile = 1;

	public static void main(String[] args) {
		CityParser(args[1], args[0]);
	}

	public static int CityParser(String outputFilePath, String InputFilePath) {
		try {
			// Opening input file creating buffer reader
			BufferedReader br = null;

			br = new BufferedReader(new FileReader(new File(InputFilePath)));

			// creating first output file

			File file = new File(outputFilePath);

			// creating the file

			file.createNewFile();

			// creating bufferedwriter
			FileWriter fw = null;
			BufferedWriter bw = null;

			fw = new FileWriter(file.getAbsoluteFile());
			bw = new BufferedWriter(fw);

			// reading the first line it is not relevant

			br.readLine();

			// reading lines
			String currLine = null;

			currLine = br.readLine();

			while (currLine != null) {

				String[] lineParts;
				lineParts = currLine.split(",");
				// parse the line and write to the file
				for (int i = 0; i < lineParts.length; i++) {
					if (i == 0) {
						int id = ParsingTools
								.GetCountryIDFromCode(lineParts[i]);

						bw.write(id + "\t");

					} else if (i == 2 || i == 3 || i == 4) {
						continue;
					} else {
						/*cleaning the string*/
						String curr = lineParts[i];
						curr = curr.replaceAll("^\"|\"$", "");
						curr = curr.replaceAll("_", " ");
						curr = curr.replaceAll("'", "''");

				
							bw.write(curr + "\t");
						
					}
				}

				bw.write("\n");

				currLine = br.readLine();

			}

			bw.write("\n");

			br.close();

			// scanner.close();

			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
		return 1;
	}
}
